<?php


namespace app\admin\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;
use think\Validate;

class Column extends Controller
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform",$platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }
    //首页栏目
    public function index(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        $name  = $status = "";
        $where = [];
        if(!empty($_GET['status'])){
            $where['status'] = $_GET['status'];
            $status = $_GET['status'];
        }
        if(!empty($_GET['name'])){
            $where['name'] = ['like',"%{$_GET['name']}%"];
            $name = $_GET['name'];
        }

        //启用状态
        $enable_status = Db::name("config_attribute")->where(['key'=>"enable_status"])->value("value");
        $enable_status = explode("|",$enable_status);
        foreach ($enable_status as $k=>$v){
            $enable_status[$k+1] = $v;
        }
        unset($enable_status[0]);
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_column")->where($where)->limit($start,$limit)->order("sort desc,id desc")->select();

                //字段拼接
                foreach ($list as $k => $v) {
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    $v['logo'] = empty($v['logo']) ? "/static/images/nopic-107.png" : $v['logo'];
                    $list[$k]['logo'] = "<img src='".$v['logo']."' style='max-height:50px;max-width:50px;'>";
                }
                $count = Db::name("admin_column")->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        $assign = [
            "name"=>$name,
            "status"=>$status
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //添加导航
    public function addColumn(){
        if(empty($_POST['id'])){
            return view("create");
        }else{
            //获取基本信息
            $detail = Db::name("admin_column")->where(['id'=>$_POST['id']])->find();
            $this->assign("detail",$detail);
            return view("update");
        }
    }

    //添加导航
    public function doColumn(){
        try{
            $common = new Common();
            $rule = [
                ["name", "require", "请填写导航名称"],
                ["logo", "require", "请上传导航图标"],
                ["link", "require", "请填写链接地址"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            //查询导航是否已存在
            if(!empty($value['id'])){
                $where['id'] = ['neq',$value['id']];
            }
            $where['name'] = $value['name'];
            $where['uniacid'] = $thisWechat['id'];
            $device = Db::name("admin_column")->where($where)->value("*");
            if(!empty($device)){
                throw new Exception("该导航已存在，请勿重复添加");
            }

            if(empty($value['id'])){
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $navid = Db::name("admin_column")->insertGetId($value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】添加平台导航【".$value['name']."】,导航id【".$navid."】" ;
            }else{
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                Db::name("admin_column")->where(['id'=>$value['id']])->update($value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】修改平台导航【".$value['name']."】,导航id【".$value['id']."】" ;
            }
            $log['controller'] = "Column";
            $log['methods'] = "doColumn";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除导航
    public function delColumn(){
        try{
            $id = $_POST['id'];
            $list = Db::name("admin_column")->where(['id'=>['in',$id]])->select();
            $res = Db::name("admin_column")->where(['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($list as $k=>$v){
                //查询小区名称
                $log['remark'] = "【" . Session::get("user_name") . "】删除平台导航【".$v['name']."】id【".$v['id']."】";
                $log['controller'] = "Column";
                $log['methods'] = "delColumn";
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}